#include "TcpUI.h"
#include <QGridLayout>
#include <QRegExpValidator>
#include <QEvent>
#include "CommShare.h"
TcpUI::TcpUI(QWidget *parent) : QWidget(parent)
{
    m_langType = 0;
    QGridLayout *grid = new QGridLayout(this);
    grid->setHorizontalSpacing(20);
    grid->setVerticalSpacing(30);
    m_labelIp = new QLabel("Ip地址:",this);
    m_labelPort = new QLabel("Ip端口:",this);
    m_labelNote = new QLabel(" ",this);
    m_editIp = new QLineEdit(this);
    m_editIp->setObjectName("IpAddr");
//    QRegExp Iprx("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
//    QRegExpValidator *IPValidator = new QRegExpValidator(Iprx, this);
//    m_editIp->setValidator(IPValidator);

    m_editPort = new QLineEdit(this);
    m_editPort->setObjectName("IpPort");

    m_Connect = new QPushButton("连接",this);
    m_Cancel = new QPushButton("取消",this);
    m_Connect->setFixedWidth(88);
     m_Cancel->setFixedWidth(88);

    grid->addWidget(m_labelIp,0,0,1,2);
    grid->addWidget(m_editIp,0,2,1,4);
    grid->addWidget(m_labelPort,1,0,1,2);
    grid->addWidget(m_editPort,1,2,1,4);

    grid->addWidget(m_Connect,2,1,1,2);
    grid->addWidget(m_Cancel,2,3,1,2);
    grid->addWidget(m_labelNote,3,1,1,4);

    m_editIp->installEventFilter(this);
    m_editPort->installEventFilter(this);

    connect(m_Cancel,&QPushButton::clicked,this,[=]()
    {
        emit SendConnectStatus(m_TcpStatus);
        this->hide();
    });

    connect(m_Connect,&QPushButton::clicked,this,[=]()
    {
        CommShared::GetInstance()->m_socketMap.clear();
        SuperSocket *socket = new SuperSocket();
        bool ok = socket->CreateClient(m_editIp->text(),m_editPort->text().toInt());
        m_TcpStatus = ok;
        emit SendConnectStatus(ok);
        if(ok)
        {
            CommShared::GetInstance()->m_socketMap["TCP"] = socket;
            this->hide();
        }
        else
        {
            m_labelNote->setText(CommShared::GetInstance()->m_LangMap[m_langType]["ConnectFail"]);
        }
    });
}

void TcpUI::SwitchLang(int type)
{
    m_langType = type;
    m_labelIp->setText(CommShared::GetInstance()->m_LangMap[type]["IpAddress"]);
    m_labelPort->setText(CommShared::GetInstance()->m_LangMap[type]["IpPort"]);
    m_Cancel->setText(CommShared::GetInstance()->m_LangMap[type]["Cancel"]);
    m_Connect->setText(CommShared::GetInstance()->m_LangMap[type]["Connect"]);
}

void TcpUI::GetWifiStatus(bool status)
{
    m_TcpStatus = status;
}

bool TcpUI::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonPress)
    {
        this->hide();
        if (watched == m_editPort)
            emit CallKeyBoard(watched->objectName(),0);
        else if (watched == m_editIp)
        {

            emit CallKeyBoard(watched->objectName(),2);
        }
    }

    return QWidget::eventFilter(watched, event);
}

void TcpUI::ReceiveKeyBoardData(const QString &objName, const QString &Data)
{
    if(objName == "IpAddr")
    {
        if(!Data.isEmpty())
        {
            QRegExp rx2("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
            if (!rx2.exactMatch(Data))
            {
                m_editIp->setText("Error IP");
                m_editIp->setStyleSheet("color:red");//文本颜色
            }
            else
            {
                m_editIp->setText(Data);
                m_editIp->setStyleSheet("color:white");//文本颜色
            }
        }
    }
    else if(objName == "IpPort")
    {
        QRegExp Portrx("(\\d+)");
        QRegExpValidator *PortValidator = new QRegExpValidator(Portrx, this);
        int pos = 0;
        QString tempdata = Data;
        PortValidator->validate(tempdata,pos);
        if(0 == pos)
            m_editPort->setText(Data);
    }
    this->show();
}
